package com.example.jacksen.alldemo.Rxbus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jacksen.alldemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.button)
    Button button;
    Subscription rxSbscription;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //接受事件
        rxSbscription=RxBus.getInstance().toObserverable(StudentEvent.class)
                .subscribe(new Action1<StudentEvent>() {
                    @Override
                    public void call(StudentEvent studentEvent) {
                        textView.setText("id:"+ studentEvent.getId()+"  name:"+ studentEvent.getName());
                    }
                });
    }
    @OnClick(R.id.button)
    public void onClick(){
        Toast.makeText(this, "挑转", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this,Main2Activity.class));
    }

    @Override
    protected void onDestroy() {
        //取消订阅
        if (!rxSbscription.isUnsubscribed()){
            rxSbscription.unsubscribe();
        }
        super.onDestroy();
    }
}
